
import Logger from 'logplease'
import * as pkg from 'uuid';

const prisma = new PrismaClient();
import { PrismaClient } from "@prisma/client";
const { v4: uuidv4 } = pkg

const logger = Logger.create('prisma')
Logger.setLogLevel('DEBUG')

export async function getOfflineInvitations(did) {
    const list = await prisma.Invitations.findMany({
        where: {
            did
        }
    });
    return list
}

export async function clearOfflineInvitations(memberDid) {
    logger.debug('clear offline invitations', memberDid)
    try {
        await prisma.Invitations.deleteMany({
            where: {
                did: memberDid
            }
        });
    } catch (err) {
        logger.debug('err when delete offline invtations', memberDid, err)
    }
}

export async function getOfflineChatMessages(did) {
    const chatMessages = await prisma.ChatMessages.findMany({
        where: {
            targetDid: did
        }
    });
    return chatMessages
}

export async function clearOfflineChatMessages(memberDid) {
    logger.debug('clear offline chat message', memberDid)
    try {
        await prisma.ChatMessages.deleteMany({
            where: {
                targetDid: memberDid
            }
        });
    } catch (err) {
        logger.debug('err when delete offline chat messages', memberDid, err)
    }
}

export async function saveChatMessage(chatMessage) {
    chatMessage.id = uuidv4()
    try {
        await prisma.ChatMessages.create({
            data: chatMessage
        });
    } catch (err) {
        logger.debug('err when save chat message', chatMessage, err)
    }
}

export async function updateInvitation(invitation) {
    try {
        const findInvite = await prisma.Invitations.findUnique({
            where: {
                id: invitation.id,
            },
        });
        if (findInvite) {
            await prisma.Invitations.update({
                where: {
                    id: invitation.id,
                },
                data: invitation
            });
        } else {
            await prisma.Invitations.create({
                data: invitation
            });
        }
    } catch (err) {
        logger.debug('err when update invitation', invitation, err)
    }
}

export async function deleteMember(memberDid) {
    try {
        await prisma.Invitations.deleteMany({
            where: {
                did: memberDid
            }
        });
        await prisma.ChatMessages.deleteMany({
            where: {
                targetDid: memberDid
            }
        });
    } catch (err) {
        logger.debug('err when delete member', memberDid, err)
    }
}

export async function updateProfile(profile) {
    logger.debug('update profile', profile)
    try {
        const findProfile = await prisma.Members.findUnique({
            where: {
                did: profile.did,
            },
        });
        if (findProfile) {
            await prisma.Members.update({
                where: {
                    did: profile.did,
                },
                data: profile
            });
        } else {
            await prisma.Members.create({
                data: profile
            });
        }
    } catch (err) {
        logger.debug('err when update profile', profile, err)
    }
}

export async function getProfile(did) {
    try {
        const findProfile = await prisma.Members.findUnique({
            where: {
                did: did,
            },
        });
        return findProfile
    } catch (err) {
        logger.debug('err when get profile', did, err)
    }
}
